package org.dromara.pay.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.pay.domain.TopUp;
import org.dromara.pay.service.TopUpService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 系统充值订单
 *
 * @author zlj
 * @date 2024-05-29
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/topUp")
public class TopUpController extends BaseController {

    private final TopUpService topUpService;

    /**
     * 查询系统充值订单列表
     */
    @SaCheckPermission("pay:topUp:list")
    @GetMapping("/list")
    public TableDataInfo<TopUp> list(TopUp bo, PageQuery pageQuery) {
        return topUpService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出系统充值订单列表
     */
    @SaCheckPermission("pay:topUp:export")
    @Log(title = "系统充值订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TopUp bo, HttpServletResponse response) {
        List<TopUp> list = topUpService.queryList(bo);
        ExcelUtil.exportExcel(list, "系统充值订单", TopUp.class, response);
    }

    /**
     * 获取系统充值订单详细信息
     *
     * @param topUpId 主键
     */
    @SaCheckPermission("pay:topUp:query")
    @GetMapping("/{topUpId}")
    public R<TopUp> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long topUpId) {
        return R.ok(topUpService.queryById(topUpId));
    }

    /**
     * 新增系统充值订单
     */
    @SaCheckPermission("pay:topUp:add")
    @Log(title = "系统充值订单", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TopUp bo) {
        return toAjax(topUpService.insertByBo(bo));
    }

    /**
     * 修改系统充值订单
     */
    @SaCheckPermission("pay:topUp:edit")
    @Log(title = "系统充值订单", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TopUp bo) {
        return toAjax(topUpService.updateByBo(bo));
    }

    /**
     * 删除系统充值订单
     *
     * @param topUpIds 主键串
     */
    @SaCheckPermission("pay:topUp:remove")
    @Log(title = "系统充值订单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{topUpIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] topUpIds) {
        return toAjax(topUpService.deleteWithValidByIds(List.of(topUpIds), true));
    }
}
